

#import "PlayerDataBase.h"
@implementation PlayerDataBase
static PlayerDataBase *playerDataBase;
+ (PlayerDataBase *)shareDataBase
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playerDataBase = [[PlayerDataBase alloc]init];
    });
    return playerDataBase;
}
//创建fmdb
- (FMDatabase *)dataBase
{
    if (!_dataBase) {
        _dataBase = [[FMDatabase alloc]init];
    }
    return _dataBase;
}

//指定数据库路径
- (NSString *)sqlitePaths:(NSString *)sqlitePaths
{
    NSString *str = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:sqlitePaths];
    NSLog(@"数据库路径%@",str);
    return str;
}
//打开数据库
- (void)openSqliteWithPaths:(NSString *)paths
{

    self.dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:paths]];

}
//关闭数据
- (void)closeSqlite
{
    [self.dataBase close];
}

// 判断是否存在表
- (BOOL) isTableOK:(NSString *)tableName
{
    FMResultSet *rs = [_dataBase executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", tableName];
    while ([rs next])
    {
        // just print out what we've got in a number of formats.
        NSInteger count = [rs intForColumn:@"count"];
        NSLog(@"isTableOK %li", count);
        
        if (0 == count)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    
    return NO;
}


//创建数据库表
- (void)createPlaySongListWithTable:(NSString *)name
{
    if (!_dataBase) {
        [self openSqliteWithPaths:@"MySql.sqlite"];
    }
    if ([_dataBase open]) {
        
        if ([self isTableOK:name]) {
            NSLog(@"表已经存在");
            return;
        }else{
        
            NSString * sql =[NSString stringWithFormat:@"CREATE TABLE %@ (ID TEXT PRIMARY KEY,title TEXT ,pubdate TEXT,original_title TEXT,image TEXT,collection TEXT)",name] ;
            
            BOOL executeUpdate = [_dataBase executeUpdate:sql];
            if (executeUpdate) {
                NSLog(@"建表成功");
            }else
            {
                NSLog(@"建表失败");
            }

        }
    }

}

//往表中插入数据(建表,插入,更新封装操作)
- (BOOL)insertIntoTable:(NSString *)tableName  Model:(WillReleasedModel *)model
{
    BOOL dataStatus = NO;
    
    if (!model) {
        NSLog(@"model为空");
        return NO;
    }
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"MySql.sqlite"]];
    }
    if ([_dataBase open]) {
        
        [self createPlaySongListWithTable:tableName];
        
        NSString *sql = [NSString stringWithFormat:@"insert into  %@  (ID,title,pubdate,original_title,image,collection) values('%@','%@','%@','%@','%@','%@')",tableName,model.ID,model.title,model.pubdate,model.original_title,model.images[@"large"],model.collection];
        NSLog(@"fgdgdhgdh%@",model.ID);
        BOOL insertModel = [_dataBase executeUpdate:sql];
        if (insertModel) {
            NSLog(@"插入成功");
            dataStatus = YES;
        }else
        {
            NSLog(@"插入失败");
            dataStatus = NO;
        }

    }
    return dataStatus;
}

//查询信息
- (NSMutableArray *)selectFromTable:(NSString *)tableName
{
    NSMutableArray *mArr = [[NSMutableArray alloc]init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"MySql.sqlite"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ ",tableName] ;
  
        
        FMResultSet *rs = [_dataBase executeQuery:sql];
        while ([rs next]) {
            WillReleasedModel *playModel = [[WillReleasedModel alloc]init];
            playModel.ID = [rs stringForColumn:@"ID"];
            playModel.title = [rs stringForColumn:@"title"];
            playModel.pubdate = [rs stringForColumn:@"pubdate"];
            playModel.original_title = [rs stringForColumn:@"original_title"];
            playModel.collection = [NSNumber numberWithInt:[[rs stringForColumn:@"collection"] intValue]];
            NSDictionary *dic = [NSDictionary dictionaryWithObject:[rs stringForColumn:@"image"] forKey:@"large"];
           
            playModel.images = [NSDictionary dictionaryWithDictionary:dic];
            [mArr addObject:playModel];
        }
    }
    return mArr;
}

//删除歌曲信息
- (void)deleteModelFromTable:(NSString *)tableName WithSongID:(NSString *)songID
{
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"MySql.sqlite"]];
    }
    if ([_dataBase open]) {
        NSString *sql = @"";
        if ([songID isEqualToString:@"All"]) {
            sql = [NSString stringWithFormat:@"delete from %@",tableName];
        }else{
        
            sql = [NSString stringWithFormat:@"delete from %@ where ID = '%@'",tableName,songID];
        }
        
        BOOL b = [_dataBase executeUpdate:sql];
        if (b) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }

    }
}
@end
